package com.lanhai.zhonglai.utils;

import java.util.ArrayList; 
import java.util.List; 

import org.springframework.stereotype.Component;

import com.lanhai.zhonglai.beans.StationTeamBean;

/**
 * 分组设备的数据容器
 * @author zhengwei
 *
 */
@Component
public class StationTeamUtils {
	  List<StationTeamBean> teamList = new ArrayList<StationTeamBean>();
	  List<Integer> mp=new ArrayList<Integer>();
	  Object locker=new Object();
	
	public void init() {
		if (teamList.size() == 0) {
			// 扩散
			teamList.add(new StationTeamBean(new int[] { 18, 20 }));
			teamList.add(new StationTeamBean(new int[] { 22, 24 }));
			teamList.add(new StationTeamBean(new int[] { 26, 28 }));
			teamList.add(new StationTeamBean(new int[] { 30, 32 }));
			teamList.add(new StationTeamBean(new int[] { 34, 36 }));
			teamList.add(new StationTeamBean(new int[] { 38, 40 }));
		 
			int[] k=new int[] {18,20,22,24,26,28,30,32,34,36,38,40} ;
			for(int i=0;i<k.length;i++) {
				mp.add(k[i]);
			}

			// ald
			teamList.add(new StationTeamBean(new int[] { 162, 164 }));
			teamList.add(new StationTeamBean(new int[] { 166, 168 }));
			teamList.add(new StationTeamBean(new int[] { 170, 172 }));
			teamList.add(new StationTeamBean(new int[] { 174, 176 }));
			teamList.add(new StationTeamBean(new int[] { 178, 180 }));
			teamList.add(new StationTeamBean(new int[] { 182, 184 }));
			k=new int[] {162,164,166,168,170,172,174,176,178,180,182,184} ;
			for(int i=0;i<k.length;i++) {
				mp.add(k[i]);
			}
			
			
			// 正膜
			teamList.add(new StationTeamBean(new int[] { 122, 124 }));
			teamList.add(new StationTeamBean(new int[] { 126, 128 }));
			teamList.add(new StationTeamBean(new int[] { 130, 132 }));
			teamList.add(new StationTeamBean(new int[] { 134, 136 }));
			teamList.add(new StationTeamBean(new int[] { 138, 140 }));
			teamList.add(new StationTeamBean(new int[] { 142, 144 }));
			teamList.add(new StationTeamBean(new int[] { 146, 148 }));

			k=new int[] { 122,124,126,128,130,132,134,136,138,140,142,144,146,148} ;
			for(int i=0;i<k.length;i++) {
				mp.add(k[i]);
			}
			
			// 背膜
			teamList.add(new StationTeamBean(new int[] { 150, 152 }));
			teamList.add(new StationTeamBean(new int[] { 154, 156 }));
			teamList.add(new StationTeamBean(new int[] { 158, 160 }));
			teamList.add(new StationTeamBean(new int[] { 106, 108 }));
			teamList.add(new StationTeamBean(new int[] { 110, 112 }));
			teamList.add(new StationTeamBean(new int[] { 114, 116 }));
			teamList.add(new StationTeamBean(new int[] { 118, 120 }));

			k=new int[] {150,152,154,156,158,160,106,108,110,112,114,116,118,120} ;
			for(int i=0;i<k.length;i++) {
				mp.add(k[i]);
			}
		 
		}
	}
	
	 /**
	  * 上锁和解锁
	 * @param flag
	 * @param dotno
	 * @param agvid
	 */
	public  void setLock(int dotno ,boolean flag) {
		if(mp.contains(dotno)==false) {
			return;
		}
		 synchronized(locker) {
			 for(int i=0;i<teamList.size();i++) {
				 StationTeamBean obj=teamList.get(i);
				 if(obj.exist(dotno)) {
					 if(flag) {
						 //上锁
						 obj.setLocked(true);
					 }else {
						 //解锁
						 obj.setLocked(false); 
					 } 
					 break;
				 }
			 }
		 } 
	 }
	
	
	public void print() {
		 for(int i=0;i<teamList.size();i++) {
			 StationTeamBean obj=teamList.get(i);
			 if(obj.isLocked()) {
				 System.out.println(""+obj.getKey()); 
			 }
			
			 
		 }
	}
	
	 /**
	  * 是否机台可以
	 * @param dotno
	 * @param agvid
	 * @return
	 */
	public boolean canUse(int dotno,int agvid) {
		if(mp.contains(dotno)==false) {
			return true;
		}
		 boolean flag=false;
		 synchronized(locker) {
			 for(int i=0;i<teamList.size();i++) { 
				 StationTeamBean obj=teamList.get(i);
				 if(obj.exist(dotno)) {
					 if(obj.isLocked()) {
						 flag=false;
					 }else {
						 flag=true;
					 } 
					 break;
				 }
			 }
		 } 
		 return flag;
	 } 
}
